/*
 * Copyright (c) 2013 WDCY Information Technology Co. Ltd
 * www.wdcy.cc
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * WDCY Information Technology Co. Ltd ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement you
 * entered into with WDCY Information Technology Co. Ltd.
 */
package com.monkey.web.platform.controller;

import com.monkey.core.domain.permission.Permission;
import com.monkey.core.domain.resource.Menu;
import com.monkey.core.domain.role.Role;
import com.monkey.core.domain.role.RoleResourcePermission;
import com.monkey.core.domain.user.User;
import com.monkey.core.plugin.dto.MonkeyDto;
import com.monkey.core.repository.search.Searchable;
import com.monkey.utils.MonkeyUtils;
import com.monkey.web.platform.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author Wu Tianqiang
 */
@Controller
@RequestMapping(value = "/m", produces = {MediaType.APPLICATION_JSON_VALUE})
public class RoleController {

    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/role", method = RequestMethod.POST)
    public String addRole(MonkeyDto monkeyDto) {
        monkeyDto.println();
        roleService.addRole(monkeyDto);
        return "SUCCESS:新增成功";
    }

    @RequestMapping(value = "/role", method = RequestMethod.PUT)
    public String modifyRole(MonkeyDto monkeyDto) {
        monkeyDto.println();
        roleService.modifyRole(monkeyDto);
        return "SUCCESS:修改成功";
    }

    @RequestMapping(value = "/role/{ids}", method = RequestMethod.DELETE)
    public String removeRole(@PathVariable Long[] ids) {
        roleService.delete(ids);
        return "SUCCESS:删除成功";
    }

    @RequestMapping(value = "/roles", method = RequestMethod.GET)
    public Page<Role> roles(Searchable searchable) {
        searchable.addSort(Sort.Direction.DESC, "createTime");
        return roleService.findAll(searchable);
    }

    /**
     * 给角色授权
     *
     * @param role
     * @param menu
     * @param permissionIds
     * @return
     */
    @RequestMapping(value = "/role/menu/permission", method = RequestMethod.POST)
    public String grantPermissionToRole(
            @RequestParam(value = "roleId") Role role,
            @RequestParam(value = "menuId") Menu menu,
            @RequestParam(value = "permissionIds") Long[] permissionIds) {
        if (MonkeyUtils.isEmpty(role)) {
            return "FAIL:角色必填";
        }
        if (MonkeyUtils.isEmpty(menu)) {
            return "FAIL:菜单资源必填";
        }
        if (MonkeyUtils.isEmpty(permissionIds)) {
            return "FAIL:权限必填";
        }
        roleService.grantPermissionToRole(role, menu, permissionIds);
        return "SUCCESS:授权成功";
    }

    /**
     * 回收角色权限
     *
     * @param role
     * @param menu
     * @param permissionIds
     * @return
     */
    @RequestMapping(value = "/role/menu/permission", method = RequestMethod.PUT)
    public String recoveryPermissionFromRole(
            @RequestParam(value = "roleId") Role role,
            @RequestParam(value = "menuId") Menu menu,
            @RequestParam(value = "permissionIds") Long[] permissionIds) {
        if (MonkeyUtils.isEmpty(role)) {
            return "FAIL:角色必填";
        }
        if (MonkeyUtils.isEmpty(menu)) {
            return "FAIL:菜单资源必填";
        }
        if (MonkeyUtils.isEmpty(permissionIds)) {
            return "FAIL:权限必填";
        }
        roleService.recoveryPermissionFromRole(role, menu, permissionIds);
        return "SUCCESS:回收权限成功";
    }

    /**
     * 通过角色和资源查询权限
     *
     * @param role
     * @param menu
     * @return
     */
    @RequestMapping(value = "/role/menu/permission", method = RequestMethod.GET)
    public List<Permission> getPermissionByRoleMenu(
            @RequestParam(value = "roleId") Role role,
            @RequestParam(value = "menuId") Menu menu) {
        return roleService.getPermissionByRoleMenu(role, menu);
    }

    /**
     * 通过角色查询权限
     *
     * @param role
     * @param pageable
     * @return
     */
    @RequestMapping(value = "/role/permission", method = RequestMethod.GET)
    public Page<RoleResourcePermission> getPermissionByRole(@RequestParam(value = "roleId") Role role, Pageable pageable) {
        return roleService.getPermissionByRole(role, pageable);
    }

    @RequestMapping(value = "/role/user", method = RequestMethod.POST)
    public String grantRoleToUser(
            @RequestParam(value = "roleIds") Long[] roleIds,
            @RequestParam(value = "userId") User user) {
        if (MonkeyUtils.isEmpty(roleIds)) {
            return "FAIL:角色必填";
        }
        if (MonkeyUtils.isEmpty(user)) {
            return "FAIL:用户必填";
        }
        roleService.grantRoleToUser(roleIds, user);
        return "SUCCESS:操作成功";
    }

    @RequestMapping(value = "/role/user", method = RequestMethod.PUT)
    public String recoveryRoleFromUser(
            @RequestParam Long[] roleIds,
            @RequestParam Long userId) {
        roleService.recoveryRoleFromUser(roleIds, userId);
        return "SUCCESS:操作成功";
    }


}